package design.other.cases;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.*;

/**
 * @program JavaBase
 * @description: 序列化玩法
 * @author: zhanglu
 * @create: 2020-03-26 17:51:00
 */
public class SerializeTest {

    private final static String FILE_ROOT_PATH = System.getProperty("user.dir") + File.separator;

    public static void main(String[] args) {

        try {
//            writeObj();
            readObj();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void readObj() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_ROOT_PATH + "obj.object"));
        Person o = (Person) ois.readObject();
        System.out.println(o.toString());
        ois.close();
    }

    public static void writeObj() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_ROOT_PATH + "obj.object"));
        oos.writeObject(new Person().setName("张三").setAge(23));
        oos.close();
    }

}

@Data
@Accessors(chain = true)
class Person implements Serializable{

    //这是每一个类模版被序列化的文件的唯一标识(相同类，相同的序列化id) //-2899190581674268899
//    private static final long serialVersionUID = 2953395068506339570l;

    private String name;
    private Integer age;

}


